import 'dart:math';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/utils/toast_util.dart';
import 'package:kq_flutter_widgets/widgets/table/kq_table.dart';
import 'package:kq_flutter_widgets/widgets/titleBar/kq_title_bar.dart';

/// 表格组件Demo 许青
class TableDemoPage2 extends StatefulWidget {
  const TableDemoPage2({super.key});

  @override
  State<StatefulWidget> createState() => _TableDemoPageState();
}

class _TableDemoPageState extends State<TableDemoPage2> {
  bool localCanRefresh = true;

  List<List<TestTableData>> _getTableTestData2() {
    //模拟数据
    List<List<TestTableData>> data = [];
    Random random = Random();

    for (int i = 0; i < 50; i++) {
      List<TestTableData> dataList = [];
      for (int j = 0; j < 30; j++) {
        int seed = random.nextInt(100);
        dataList.add(TestTableData(" $seed "));
      }
      data.add(dataList);
    }
    return data;
  }

  final EasyRefreshController easyRefreshController = EasyRefreshController(
      controlFinishRefresh: true, controlFinishLoad: true);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: KqHeadBar(
        headTitle: "表格控件演示2",
      ),
      body: EasyRefresh.builder(
          controller: easyRefreshController,
          refreshOnStart: true,
          onRefresh: () => easyRefreshController.finishRefresh(),
          onLoad: () {
            easyRefreshController.finishLoad();
          },
          childBuilder: (BuildContext context, ScrollPhysics physics) {
            return StatefulBuilder(
              builder: (BuildContext context,
                  void Function(void Function()) setState) {
                return CustomScrollView(
                    physics: localCanRefresh ? physics : null,
                    slivers: [
                      SliverFillRemaining(
                        child: Column(children: [
                          SizedBox(
                            width: 300,
                            height: 100,
                            child: Container(
                              color: Colors.red,
                              child: const Text("测试顶部添加文字"),
                            ),
                          ),
                          Expanded(
                              child: KqTable<TestTableData>(
                            width: 300,
                            height: 400,
                            data: _getTableTestData2(),
                            onTap: (TestTableData data) {
                              KqToast.showNormal(data.text);
                            },
                            verticalScroll: (canVerticalScroll) {
                              localCanRefresh = canVerticalScroll;
                              setState(() {});
                            },
                            inertiaScrollToDirection: (direction) {},
                          )),
                          SizedBox(
                            width: 300,
                            height: 100,
                            child: Container(
                              color: Colors.green,
                              child: const Text("测试底部添加文字"),
                            ),
                          ),
                        ]),
                      ),
                    ]);
              },
            );
          }),
    );
  }
}

class TestTableData extends ITableData {
  TestTableData(super.text);
}
